﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Net;
using System.Net.Sockets;
namespace Share.Utilities
{
   public static class SysInfo
    {
        #region HDDNumID
        private static string _HDDNumID = string.Empty;
        public static string HDDNumID
        {
            get
            {
                if (_HDDNumID == string.Empty)
                    GetHDDNum_1();
                return _HDDNumID;
            }
        }
        #region Private
        private static void GetHDDNum_1()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            try
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    if (share.Properties.Count <= 0)
                    {
                        Console.Write("No Information Available");
                        return;
                    }
                    foreach (PropertyData PC in share.Properties)
                    {
                        if (PC.Name == "SerialNumber")
                        {
                            _HDDNumID = PC.Value == null ? string.Empty : PC.Value.ToString();
                            if (string.IsNullOrEmpty(_HDDNumID) == false)
                                return;
                        }
                    }
                }
            }
            catch
            {
            }
        }
        #endregion
        #endregion

        #region CPUNumID
        public static string CPUNumID
        {
            get
            {
                return GetCPUNumID_1();
            }
        }
        #region Private
        private static string GetCPUNumID_1()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            try
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    if (share.Properties.Count <= 0)
                    {
                        Console.Write("No Information Available");
                        return "";
                    }
                    foreach (PropertyData PC in share.Properties)
                    {
                        string _CPUNumID = "";
                        if (PC.Name == "ProcessorId")
                        {
                            _CPUNumID = PC.Value == null ? string.Empty : PC.Value.ToString();
                            if (string.IsNullOrEmpty(_CPUNumID) == false)
                                return _CPUNumID;
                        }
                    }
                }
            }
            catch
            {
            }
            return "";
        }
        #endregion
        #endregion

        #region IpAddress
        private static string GetIpAddress()
        {
            try
            {
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                // test if any host IP equals to any local IP or to localhost
                foreach (IPAddress localIP in localIPs)
                {
                    if (localIP.AddressFamily == AddressFamily.InterNetwork)
                        return localIP.ToString();
                }
            }
            catch { }
            return string.Empty;
        }
        static string _IpAddress;
        public static string IpAddress
        {
            get
            {
                if (string.IsNullOrEmpty(_IpAddress))
                    _IpAddress = GetIpAddress();
                return _IpAddress;
            }
        }
        #endregion

        #region ComputerName
        private static string GetComputerName()
        {
            try
            {
                return Dns.GetHostName();
            }
            catch { }
            return string.Empty;
        }
        static string _ComputerName;
        public static string ComputerName
        {
            get
            {
                if (string.IsNullOrEmpty(_ComputerName))
                    _ComputerName = GetComputerName();
                return _ComputerName;
            }
        }
        #endregion
    }
}
